#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#define THREAD_POOL_SIZE 4
#define QUEUE_SIZE 100

typedef struct {
    void (*function)(void *);
    void *argument;
} task_t;

// 1. 任务队列：task_queue_t结构体用于存储任务队列，使用环形缓冲区实现。
typedef struct {
    task_t tasks[QUEUE_SIZE];
    // front和rear分别指向队列的头和尾，count表示当前队列中的任务数量。
    int front;
    int rear;
    int count;
    pthread_mutex_t lock;
    pthread_cond_t notify;
} task_queue_t; 


// 2. 线程池：thread_pool_t结构体包含线程数组、任务队列和一个标志shutdown
typedef struct {
    pthread_t *threads;
    task_queue_t *queue;
    int shutdown; //用于指示线程池是否关闭。
} thread_pool_t;

// 3. 工作线程：worker_thread函数是每个线程的执行函数，它会不断从任务队列中取出任务并执行。
void *worker_thread(void *arg) {
    thread_pool_t *pool = (thread_pool_t *)arg;
    while (1) {
        pthread_mutex_lock(&(pool->queue->lock));
        while (pool->queue->count == 0 && !pool->shutdown) {
            pthread_cond_wait(&(pool->queue->notify), &(pool->queue->lock));
        }
        if (pool->shutdown) {
            pthread_mutex_unlock(&(pool->queue->lock));
            pthread_exit(NULL);
        }
        task_t task = pool->queue->tasks[pool->queue->front];
        pool->queue->front = (pool->queue->front + 1) % QUEUE_SIZE;
        pool->queue->count--;

        pthread_mutex_unlock(&(pool->queue->lock));

        (*(task.function))(task.argument);
    }
    return NULL;
}

// 4. 线程池创建：thread_pool_create函数创建线程池，初始化任务队列，并创建指定数量的线程。
thread_pool_t *thread_pool_create(int num_threads) {
    thread_pool_t *pool = (thread_pool_t *)malloc(sizeof(thread_pool_t));
    pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * num_threads);
    pool->queue = (task_queue_t *)malloc(sizeof(task_queue_t));

    pool->queue->front = 0;
    pool->queue->rear = 0;
    pool->queue->count = 0;
    pthread_mutex_init(&(pool->queue->lock), NULL);
    pthread_cond_init(&(pool->queue->notify), NULL);

    pool->shutdown = 0;

    for (int i = 0; i < num_threads; i++) {
        pthread_create(&(pool->threads[i]), NULL, worker_thread, (void *)pool);
    }

    return pool;
}

// 5. 添加任务：thread_pool_add_task函数向任务队列中添加任务，并通知等待的线程。
void thread_pool_add_task(thread_pool_t *pool, void (*function)(void *), void *argument) {
    pthread_mutex_lock(&(pool->queue->lock));

    if (pool->queue->count == QUEUE_SIZE) {
        pthread_mutex_unlock(&(pool->queue->lock));
        return;
    }

    pool->queue->tasks[pool->queue->rear].function = function;
    pool->queue->tasks[pool->queue->rear].argument = argument;
    pool->queue->rear = (pool->queue->rear + 1) % QUEUE_SIZE;
    pool->queue->count++;

    pthread_cond_signal(&(pool->queue->notify));
    pthread_mutex_unlock(&(pool->queue->lock));
}

// 6. 销毁线程池：thread_pool_destroy函数用于销毁线程池，等待所有线程退出并释放资源。
void thread_pool_destroy(thread_pool_t *pool) {
    pthread_mutex_lock(&(pool->queue->lock));
    pool->shutdown = 1;
    pthread_cond_broadcast(&(pool->queue->notify));
    pthread_mutex_unlock(&(pool->queue->lock));

    for (int i = 0; i < THREAD_POOL_SIZE; i++) {
        pthread_join(pool->threads[i], NULL);
    }

    free(pool->threads);
    free(pool->queue);
    free(pool);
}

// 7. 示例任务：example_task是一个简单的任务函数，打印传入的参数并休眠1秒。
void example_task(void *arg) {
    int *num = (int *)arg;
    printf("Task executed with argument: %d\n", *num);
    sleep(1);
}

int main() {
    thread_pool_t *pool = thread_pool_create(THREAD_POOL_SIZE);

    int task_args[10];
    for (int i = 0; i < 10; i++) {
        task_args[i] = i;
        thread_pool_add_task(pool, example_task, (void *)&task_args[i]);
    }

    sleep(5); // Wait for tasks to complete

    thread_pool_destroy(pool);

    return 0;
}




